package org.ft.fileConvert.utils;

import cn.hutool.core.io.FileUtil;
import com.aspose.words.*;
import javassist.Modifier;
import lombok.extern.slf4j.Slf4j;
import org.ofd.render.OFDRender;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * Created by Chen Tianzhong in 2022/8/10
 */
@Component
@Slf4j
public class FileConvertUtil {

    public void convertWordToPdf(String wordFilePath,String pdfFilePath) throws Exception {
        FileOutputStream os = null;
        try {
            log.debug("开始转换PDF文件{}到{}",wordFilePath,pdfFilePath);
//            InputStream is = FileConvertUtil.class.getClassLoader().getResourceAsStream("license.xml");
//            License license = new License();
//            license.setLicense(is);
            try{
//                Class<?> aClass = Class.forName("com.aspose.words.zzXyu");
//                java.lang.reflect.Field zzYAC = aClass.getDeclaredField("zzZXG");
//                zzYAC.setAccessible(true);
//                java.lang.reflect.Field modifiersField = zzYAC.getClass().getDeclaredField("modifiers");
//                modifiersField.setAccessible(true);
//                modifiersField.setInt(zzYAC, zzYAC.getModifiers() & ~Modifier.FINAL);
//                zzYAC.set(null,new byte[]{76, 73, 67, 69, 78, 83, 69, 68});

//
                Class<?> zzjXClass = Class.forName("com.aspose.words.zzXvM");
                Constructor<?> constructor = zzjXClass.getDeclaredConstructors()[0];
                constructor.setAccessible(true);
                Object zzjXInstance = constructor.newInstance();

                // zzZ7O
                java.lang.reflect.Field zzZ7O = zzjXClass.getDeclaredField("zzhP");
                zzZ7O.setAccessible(true);
                zzZ7O.set(zzjXInstance, new Date(Long.MAX_VALUE));

                // zzBf
                java.lang.reflect.Field zzZfB = zzjXClass.getDeclaredField("zzEJ");
                zzZfB.setAccessible(true);
                Class<?> zzYP3Class = Class.forName("com.aspose.words.zzXui");
                java.lang.reflect.Field zzBfField = zzYP3Class.getDeclaredField("zzXed");
                zzBfField.setAccessible(true);
                zzZfB.set(zzjXInstance, zzBfField.get(null));

                // zzZjA
                java.lang.reflect.Field zzZjA = zzjXClass.getDeclaredField("zzKy");
                zzZjA.setAccessible(true);
                zzZjA.set(null, zzjXInstance);


                Class<?> zzCnClass = Class.forName("com.aspose.words.zzVOo");
                java.lang.reflect.Field zzZyx = zzCnClass.getDeclaredField("zzZtE");
                zzZyx.setAccessible(true);
                zzZyx.set(null, 128);
                java.lang.reflect.Field zzZ8w = zzCnClass.getDeclaredField("zzZ3p");
                zzZ8w.setAccessible(true);
                zzZ8w.set(null, false);
            }catch (Exception e){
                System.out.println("apose word 破解异常");
            }


            File file = new File(pdfFilePath);
            if (!file.exists()){
                FileUtil.touch(file);
            }
            os = new FileOutputStream(file);

//            FontSettings fontSettings = new FontSettings();
//            String property = System.getProperty("os.name");
//            if (property != null && property.toLowerCase().startsWith("windows")) {
//                //这里要求的字体是给全部用户安装，不是用户的自己字体
//                fontSettings.setFontsFolders(new String[]{"C:\\Windows\\Fonts"}, true);
//            } else if (property != null && property.toLowerCase().startsWith("linux")) {//Linux操作系统
//                fontSettings.setFontsFolders(new String[]{"/usr/share/fonts"}, true);
//            } else{
//                //mac电脑不处理，没见过
//            }

            Document docxFile = new Document(wordFilePath);
            SaveOutputParameters save = docxFile.save(pdfFilePath, SaveFormat.PDF);//还不如这个好用，转换效果稳定
            String contentType = save.getContentType();

//            Document document = new Document();//新建一个空白pdf文档
//            document.removeAllChildren();
//            document.appendDocument(docxFile, ImportFormatMode.USE_DESTINATION_STYLES);//保留样式
//            TableCollection tables = document.getFirstSection().getBody().getTables();
//            for (Table table : tables) {
//                RowCollection rows = table.getRows();
//                table.setAllowAutoFit(false);
//                for (Row row : rows) {
//                    CellCollection cells = row.getCells();
//                    for (Cell cell : cells) {
//                        CellFormat cellFormat = cell.getCellFormat();
//                        cellFormat.setFitText(false);
//                        cellFormat.setWrapText(true);
//                    }
//                }
//            }
//            document.save(os, SaveFormat.PDF);
        }catch (Exception e){
            log.error("word转pdf失败！！！");
            log.error(e.getLocalizedMessage(), e);
            throw e;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void convertPdfToOfd(String pdfFilePath,String ofdFilePath) throws Exception {
        Path pdfIn = Paths.get(pdfFilePath);
        Path ofdout = Paths.get(ofdFilePath);
        try {
            log.debug("开始转换OFD文件{}到{}",pdfFilePath,ofdFilePath);

            OFDRender.convertPdfToOfd(Files.newInputStream(pdfIn), Files.newOutputStream(ofdout));
        }catch (Exception e){
            log.error("pdf转ofd失败！！！");
            log.error(e.getLocalizedMessage(), e);
            throw e;
        }
    }
}
